﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Configuration;
using System.Reflection;
using IExternalDataTypes;
using ExaminationTT.Control;

namespace ReflAndFileExplorer
{
    public partial class Form1 : Form
    {
        private readonly ToolStripItem[] _toolItems;
        private readonly LoaderLib.ILoader _l;
        private IEnumerable<IStudent> _ls;
        private IEnumerable<ICUnit> _luc;
        private IEnumerable<ICourse> _lc;
        private IEnumerable<IClass> _lcl;
        private IEnumerable<ISemester> _lsm;
        private IEnumerable<IRoom> _lr;
        private IEnumerable<ITeacher> _lt;
        private IEnumerable<IRegime> _lre;

        private HostApplication _host;

        public void SetHostApplication(HostApplication host)
        {
            _host = host;
        }
        public Form1()
        {
            
            InitializeComponent();
            var loaderPath = ConfigurationManager.AppSettings["LoadersPath"];
            var filePath = Directory.GetFiles(loaderPath);
            _ls = new List<IStudent>();
            _luc = new List<ICUnit>();
            _toolItems = new ToolStripItem[3];
            foreach (var s in filePath)
            {
                FileInfo f = new FileInfo(s);
                if(f.Extension.Equals(".dll"))
                {
                    Assembly a = Assembly.LoadFrom(f.FullName);
                    Type[] ta = a.GetExportedTypes();
                    foreach (var type in ta)
                    {
                        if (typeof(LoaderLib.ILoader).IsAssignableFrom(type))
                        {
                            _l = (LoaderLib.ILoader)Activator.CreateInstance(type, new object[] { this });
                            _toolItems[0] = new ToolStripButton(_l.Getname(), null, (sender, e) => GetData());
                        }
                    }
                }
            }
           
            //_toolStrip = new ToolStripItemCollection()
            //_toolItems[0]=(new ToolStripButton("Hello",null, (sender, e) => MessageBox.Show("Hello")));
            _toolItems[1]=(new ToolStripButton("World",null,(sender,e)=> MessageBox.Show("World")));
            _toolItems[2] = (new ToolStripButton("Items", null, (sender, e) => MessageBox.Show("Items")));
            Loader.Items.Add(new ToolStripMenuItem("Loader", null, _toolItems));
        }

        private void GetData()
        {
            _l.DataSource();
            _host.SetLoader(_l);
            _host.Setup();
            //_ls = _l.GetStudents();
            //_luc = _l.GetCUnit();
            //_lc = _l.GetCourse();
            //_lcl = _l.GetClass();
            //_lr = _l.GetRoom();
            //_lsm = _l.GetSemester();
            //_lt = _l.GetTeacher();
            //_lre = _l.GetRegime();
        }

        private void FExplorer_Click(object sender, EventArgs e)
        {
            var fb = new OpenFileDialog {Filter = @"Excel files (*.xls)|*.xls"};
            fb.ShowDialog(this);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ;
        }
    }
}
